package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.ProfesIndicatorOne;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.ProfesIndicatorOneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理毕业生能力要求一级指标信息")
@CrossOrigin
@RestController
@RequestMapping("/ProfesIndicatorOnes")
public class ProfesIndicatorOneController {

    @Autowired
    private ProfesIndicatorOneService profesIndicatorOneService;

    @ApiOperation(value = "分页查询一级指标列表")
    @GetMapping("/AllProfesIndicatorOne")
    public String getAllIndicatorOneList(QueryInfo queryInfo) {
        int numbers = profesIndicatorOneService.getIndicatorOneCounts(queryInfo.getQuery());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<ProfesIndicatorOne> profesIndicatorOnes = profesIndicatorOneService.getAllIndicatorOneList(queryInfo.getQuery(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data", profesIndicatorOnes);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加一级指标")
    @PostMapping("/ProfesIndicatorOne")
    public String addIndicatorOne(@RequestBody ProfesIndicatorOne profesIndicatorOne) {
        int result;
        try{
            result = profesIndicatorOneService.addIndicatorOne(profesIndicatorOne);
        }
        catch(Exception e) {
            result = 0;
        }
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "删除指定一级指标")
    @DeleteMapping("/ProfesIndicatorOne")
    public String deleteIndicatorOne(String findicname) {
        int result = profesIndicatorOneService.deleteIndicatorOne(findicname);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "查询指定一级指标信息")
    @GetMapping("/ProfesIndicatorOneMessage")
    public String getUpdateIndicatorOne(String findicname) {
        ProfesIndicatorOne updateProfesIndicatorOne = profesIndicatorOneService.getUpdateIndicatorOne(findicname);
        String users_json = JSON.toJSONString(updateProfesIndicatorOne);
        return users_json;
    }

    @ApiOperation(value = "更新指定一级指标信息")
    @PutMapping("/ProfesIndicatorOne")
    public String updateIndicatorOne(@RequestBody ProfesIndicatorOne profesIndicatorOne) {
        int result = profesIndicatorOneService.updateIndicatorOne(profesIndicatorOne);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }
}
